<?php
/*
	This SQL query will create the table to store your object.

	CREATE TABLE `contact` (
	`contactid` int(11) NOT NULL auto_increment,
	`salutation` VARCHAR(255) NOT NULL,
	`lastname` VARCHAR(255) NOT NULL,
	`firstname` VARCHAR(255) NOT NULL,
	`birthdate` DATE NOT NULL,
	`jobtitle` VARCHAR(255) NOT NULL,
	`email` VARCHAR(255) NOT NULL, PRIMARY KEY  (`contactid`)) ENGINE=MyISAM;
*/

/**
* <b>Contact</b> class with integrated CRUD methods.
* @author Php Object Generator
* @version POG 3.0f / PHP5.1 MYSQL
* @see http://www.phpobjectgenerator.com/plog/tutorials/45/pdo-mysql
* @copyright Free for personal & commercial use. (Offered under the BSD license)
* @link http://www.phpobjectgenerator.com/?language=php5.1&wrapper=pdo&pdoDriver=mysql&objectName=Contact&attributeList=array+%28%0A++0+%3D%3E+%27salutation%27%2C%0A++1+%3D%3E+%27lastname%27%2C%0A++2+%3D%3E+%27firstname%27%2C%0A++3+%3D%3E+%27birthdate%27%2C%0A++4+%3D%3E+%27jobtitle%27%2C%0A++5+%3D%3E+%27Address%27%2C%0A++6+%3D%3E+%27Phone%27%2C%0A++7+%3D%3E+%27email%27%2C%0A++8+%3D%3E+%27Document%27%2C%0A++9+%3D%3E+%27GotStudentActivity%27%2C%0A++10+%3D%3E+%27Company%27%2C%0A++11+%3D%3E+%27GotProRole%27%2C%0A++12+%3D%3E+%27Activity%27%2C%0A++13+%3D%3E+%27CRMUser%27%2C%0A++14+%3D%3E+%27SupportCase%27%2C%0A%29&typeList=array%2B%2528%250A%2B%2B0%2B%253D%253E%2B%2527VARCHAR%2528255%2529%2527%252C%250A%2B%2B1%2B%253D%253E%2B%2527VARCHAR%2528255%2529%2527%252C%250A%2B%2B2%2B%253D%253E%2B%2527VARCHAR%2528255%2529%2527%252C%250A%2B%2B3%2B%253D%253E%2B%2527DATE%2527%252C%250A%2B%2B4%2B%253D%253E%2B%2527VARCHAR%2528255%2529%2527%252C%250A%2B%2B5%2B%253D%253E%2B%2527JOIN%2527%252C%250A%2B%2B6%2B%253D%253E%2B%2527JOIN%2527%252C%250A%2B%2B7%2B%253D%253E%2B%2527VARCHAR%2528255%2529%2527%252C%250A%2B%2B8%2B%253D%253E%2B%2527JOIN%2527%252C%250A%2B%2B9%2B%253D%253E%2B%2527HASMANY%2527%252C%250A%2B%2B10%2B%253D%253E%2B%2527HASMANY%2527%252C%250A%2B%2B11%2B%253D%253E%2B%2527HASMANY%2527%252C%250A%2B%2B12%2B%253D%253E%2B%2527HASMANY%2527%252C%250A%2B%2B13%2B%253D%253E%2B%2527HASMANY%2527%252C%250A%2B%2B14%2B%253D%253E%2B%2527HASMANY%2527%252C%250A%2529
*/
include_once('class.pog_base.php');
include_once('class.addresscontactmap.php');
include_once('class.contactphonemap.php');
include_once('class.contactdocumentmap.php');
class Contact extends POG_Base
{
	public $contactId = '';

	/**
	 * @var VARCHAR(255)
	 */
	public $salutation;
	
	/**
	 * @var VARCHAR(255)
	 */
	public $lastname;
	
	/**
	 * @var VARCHAR(255)
	 */
	public $firstname;
	
	/**
	 * @var DATE
	 */
	public $birthdate;
	
	/**
	 * @var VARCHAR(255)
	 */
	public $jobtitle;
	
	/**
	 * @var private array of Address objects
	 */
	private $_addressList = array();
	
	/**
	 * @var private array of Phone objects
	 */
	private $_phoneList = array();
	
	/**
	 * @var VARCHAR(255)
	 */
	public $email;
	
	/**
	 * @var private array of Document objects
	 */
	private $_documentList = array();
	
	/**
	 * @var private array of GotStudentActivity objects
	 */
	private $_gotstudentactivityList = array();
	
	/**
	 * @var private array of Company objects
	 */
	private $_companyList = array();
	
	/**
	 * @var private array of GotProRole objects
	 */
	private $_gotproroleList = array();
	
	/**
	 * @var private array of Activity objects
	 */
	private $_activityList = array();
	
	/**
	 * @var private array of CRMUser objects
	 */
	private $_crmuserList = array();
	
	/**
	 * @var private array of SupportCase objects
	 */
	private $_supportcaseList = array();
	
	public $pog_attribute_type = array(
		"contactId" => array('db_attributes' => array("NUMERIC", "INT")),
		"salutation" => array('db_attributes' => array("TEXT", "VARCHAR", "255")),
		"lastname" => array('db_attributes' => array("TEXT", "VARCHAR", "255")),
		"firstname" => array('db_attributes' => array("TEXT", "VARCHAR", "255")),
		"birthdate" => array('db_attributes' => array("NUMERIC", "DATE")),
		"jobtitle" => array('db_attributes' => array("TEXT", "VARCHAR", "255")),
		"Address" => array('db_attributes' => array("OBJECT", "JOIN")),
		"Phone" => array('db_attributes' => array("OBJECT", "JOIN")),
		"email" => array('db_attributes' => array("TEXT", "VARCHAR", "255")),
		"Document" => array('db_attributes' => array("OBJECT", "JOIN")),
		"GotStudentActivity" => array('db_attributes' => array("OBJECT", "HASMANY")),
		"Company" => array('db_attributes' => array("OBJECT", "HASMANY")),
		"GotProRole" => array('db_attributes' => array("OBJECT", "HASMANY")),
		"Activity" => array('db_attributes' => array("OBJECT", "HASMANY")),
		"CRMUser" => array('db_attributes' => array("OBJECT", "HASMANY")),
		"SupportCase" => array('db_attributes' => array("OBJECT", "HASMANY")),
		);
	public $pog_query;
	
	
	/**
	* Getter for some private attributes
	* @return mixed $attribute
	*/
	public function __get($attribute)
	{
		if (isset($this->{"_".$attribute}))
		{
			return $this->{"_".$attribute};
		}
		else
		{
			return false;
		}
	}
	
	function Contact($salutation='', $lastname='', $firstname='', $birthdate='', $jobtitle='', $email='')
	{
		$this->salutation = $salutation;
		$this->lastname = $lastname;
		$this->firstname = $firstname;
		$this->birthdate = $birthdate;
		$this->jobtitle = $jobtitle;
		$this->_addressList = array();
		$this->_phoneList = array();
		$this->email = $email;
		$this->_documentList = array();
		$this->_gotstudentactivityList = array();
		$this->_companyList = array();
		$this->_gotproroleList = array();
		$this->_activityList = array();
		$this->_crmuserList = array();
		$this->_supportcaseList = array();
	}
	
	
	/**
	* Gets object from database
	* @param integer $contactId 
	* @return object $Contact
	*/
	function Get($contactId)
	{
		$connection = Database::Connect();
		$this->pog_query = "select * from `contact` where `contactid`='".intval($contactId)."' LIMIT 1";
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$this->contactId = $row['contactid'];
			$this->salutation = $this->Unescape($row['salutation']);
			$this->lastname = $this->Unescape($row['lastname']);
			$this->firstname = $this->Unescape($row['firstname']);
			$this->birthdate = $row['birthdate'];
			$this->jobtitle = $this->Unescape($row['jobtitle']);
			$this->email = $this->Unescape($row['email']);
		}
		return $this;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $contactList
	*/
	function GetList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$connection = Database::Connect();
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$this->pog_query = "select * from `contact` ";
		$contactList = Array();
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " where ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($this->pog_attribute_type[$sortBy]['db_attributes']) && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE($sortBy) ";
				}
				else
				{
					$sortBy = "$sortBy ";
				}
			}
			else
			{
				$sortBy = "$sortBy ";
			}
		}
		else
		{
			$sortBy = "contactid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$thisObjectName = get_class($this);
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$contact = new $thisObjectName();
			$contact->contactId = $row['contactid'];
			$contact->salutation = $this->Unescape($row['salutation']);
			$contact->lastname = $this->Unescape($row['lastname']);
			$contact->firstname = $this->Unescape($row['firstname']);
			$contact->birthdate = $row['birthdate'];
			$contact->jobtitle = $this->Unescape($row['jobtitle']);
			$contact->email = $this->Unescape($row['email']);
			$contactList[] = $contact;
		}
		return $contactList;
	}
	
	
	/**
	* Saves the object to the database
	* @return integer $contactId
	*/
	function Save($deep = true)
	{
		$connection = Database::Connect();
		$this->pog_query = "select `contactid` from `contact` where `contactid`='".$this->contactId."' LIMIT 1";
		$rows = Database::Query($this->pog_query, $connection);
		if ($rows > 0)
		{
			$this->pog_query = "update `contact` set 
			`salutation`='".$this->Escape($this->salutation)."', 
			`lastname`='".$this->Escape($this->lastname)."', 
			`firstname`='".$this->Escape($this->firstname)."', 
			`birthdate`='".$this->birthdate."', 
			`jobtitle`='".$this->Escape($this->jobtitle)."', 
			`email`='".$this->Escape($this->email)."'where `contactid`='".$this->contactId."'";
		}
		else
		{
			$this->pog_query = "insert into `contact` (`salutation`, `lastname`, `firstname`, `birthdate`, `jobtitle`, `email`) values (
			'".$this->Escape($this->salutation)."', 
			'".$this->Escape($this->lastname)."', 
			'".$this->Escape($this->firstname)."', 
			'".$this->birthdate."', 
			'".$this->Escape($this->jobtitle)."', 
			'".$this->Escape($this->email)."')";
		}
		$insertId = Database::InsertOrUpdate($this->pog_query, $connection);
		if ($this->contactId == "")
		{
			$this->contactId = $insertId;
		}
		if ($deep)
		{
			foreach ($this->_addressList as $address)
			{
				$address->Save();
				$map = new AddressContactMap();
				$map->AddMapping($this, $address);
			}
			foreach ($this->_phoneList as $phone)
			{
				$phone->Save();
				$map = new ContactPhoneMap();
				$map->AddMapping($this, $phone);
			}
			foreach ($this->_documentList as $document)
			{
				$document->Save();
				$map = new ContactDocumentMap();
				$map->AddMapping($this, $document);
			}
			foreach ($this->_gotstudentactivityList as $gotstudentactivity)
			{
				$gotstudentactivity->contactId = $this->contactId;
				$gotstudentactivity->Save($deep);
			}
			foreach ($this->_companyList as $company)
			{
				$company->contactId = $this->contactId;
				$company->Save($deep);
			}
			foreach ($this->_gotproroleList as $gotprorole)
			{
				$gotprorole->contactId = $this->contactId;
				$gotprorole->Save($deep);
			}
			foreach ($this->_activityList as $activity)
			{
				$activity->contactId = $this->contactId;
				$activity->Save($deep);
			}
			foreach ($this->_crmuserList as $crmuser)
			{
				$crmuser->contactId = $this->contactId;
				$crmuser->Save($deep);
			}
			foreach ($this->_supportcaseList as $supportcase)
			{
				$supportcase->contactId = $this->contactId;
				$supportcase->Save($deep);
			}
		}
		return $this->contactId;
	}
	
	
	/**
	* Clones the object and saves it to the database
	* @return integer $contactId
	*/
	function SaveNew($deep = false)
	{
		$this->contactId = '';
		return $this->Save($deep);
	}
	
	
	/**
	* Deletes the object from the database
	* @return boolean
	*/
	function Delete($deep = false, $across = false)
	{
		if ($deep)
		{
			$gotstudentactivityList = $this->GetGotstudentactivityList();
			foreach ($gotstudentactivityList as $gotstudentactivity)
			{
				$gotstudentactivity->Delete($deep, $across);
			}
			$companyList = $this->GetCompanyList();
			foreach ($companyList as $company)
			{
				$company->Delete($deep, $across);
			}
			$gotproroleList = $this->GetGotproroleList();
			foreach ($gotproroleList as $gotprorole)
			{
				$gotprorole->Delete($deep, $across);
			}
			$activityList = $this->GetActivityList();
			foreach ($activityList as $activity)
			{
				$activity->Delete($deep, $across);
			}
			$crmuserList = $this->GetCrmuserList();
			foreach ($crmuserList as $crmuser)
			{
				$crmuser->Delete($deep, $across);
			}
			$supportcaseList = $this->GetSupportcaseList();
			foreach ($supportcaseList as $supportcase)
			{
				$supportcase->Delete($deep, $across);
			}
		}
		if ($across)
		{
			$addressList = $this->GetAddressList();
			$map = new AddressContactMap();
			$map->RemoveMapping($this);
			foreach ($addressList as $address)
			{
				$address->Delete($deep, $across);
			}
			$phoneList = $this->GetPhoneList();
			$map = new ContactPhoneMap();
			$map->RemoveMapping($this);
			foreach ($phoneList as $phone)
			{
				$phone->Delete($deep, $across);
			}
			$documentList = $this->GetDocumentList();
			$map = new ContactDocumentMap();
			$map->RemoveMapping($this);
			foreach ($documentList as $document)
			{
				$document->Delete($deep, $across);
			}
		}
		else
		{
			$map = new AddressContactMap();
			$map->RemoveMapping($this);
			$map = new ContactPhoneMap();
			$map->RemoveMapping($this);
			$map = new ContactDocumentMap();
			$map->RemoveMapping($this);
		}
		$connection = Database::Connect();
		$this->pog_query = "delete from `contact` where `contactid`='".$this->contactId."'";
		return Database::NonQuery($this->pog_query, $connection);
	}
	
	
	/**
	* Deletes a list of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param bool $deep 
	* @return 
	*/
	function DeleteList($fcv_array, $deep = false, $across = false)
	{
		if (sizeof($fcv_array) > 0)
		{
			if ($deep || $across)
			{
				$objectList = $this->GetList($fcv_array);
				foreach ($objectList as $object)
				{
					$object->Delete($deep, $across);
				}
			}
			else
			{
				$connection = Database::Connect();
				$pog_query = "delete from `contact` where ";
				for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
				{
					if (sizeof($fcv_array[$i]) == 1)
					{
						$pog_query .= " ".$fcv_array[$i][0]." ";
						continue;
					}
					else
					{
						if ($i > 0 && sizeof($fcv_array[$i-1]) !== 1)
						{
							$pog_query .= " AND ";
						}
						if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
						{
							$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$this->Escape($fcv_array[$i][2])."'";
						}
						else
						{
							$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$fcv_array[$i][2]."'";
						}
					}
				}
				return Database::NonQuery($pog_query, $connection);
			}
		}
	}
	
	
	/**
	* Creates mappings between this and all objects in the Address List array. Any existing mapping will become orphan(s)
	* @return null
	*/
	function SetAddressList(&$addressList)
	{
		$map = new AddressContactMap();
		$map->RemoveMapping($this);
		$this->_addressList = $addressList;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $contactList
	*/
	function GetAddressList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$connection = Database::Connect();
		$address = new Address();
		$addressList = Array();
		$this->pog_query = "select distinct * from `address` a INNER JOIN `addresscontactmap` m ON m.addressid = a.addressid where m.contactid = '$this->contactId' ";
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " AND ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($address->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $address->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $address->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($address->pog_attribute_type[$sortBy]['db_attributes']) && $address->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $address->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE(a.$sortBy) ";
				}
				else
				{
					$sortBy = "a.$sortBy ";
				}
			}
			else
			{
				$sortBy = "a.$sortBy ";
			}
		}
		else
		{
			$sortBy = "a.addressid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$cursor = Database::Reader($this->pog_query, $connection);
		while($rows = Database::Read($cursor))
		{
			$address = new Address();
			foreach ($address->pog_attribute_type as $attribute_name => $attrubute_type)
			{
				if ($attrubute_type['db_attributes'][1] != "HASMANY" && $attrubute_type['db_attributes'][1] != "JOIN")
				{
					if ($attrubute_type['db_attributes'][1] == "BELONGSTO")
					{
						$address->{strtolower($attribute_name).'Id'} = $rows[strtolower($attribute_name).'id'];
						continue;
					}
					$address->{$attribute_name} = $this->Unescape($rows[strtolower($attribute_name)]);
				}
			}
			$addressList[] = $address;
		}
		return $addressList;
	}
	
	
	/**
	* Associates the Address object to this one
	* @return 
	*/
	function AddAddress(&$address)
	{
		if ($address instanceof Address)
		{
			if (in_array($this, $address->contactList, true))
			{
				return false;
			}
			else
			{
				$found = false;
				foreach ($this->_addressList as $address2)
				{
					if ($address->addressId > 0 && $address->addressId == $address2->addressId)
					{
						$found = true;
						break;
					}
				}
				if (!$found)
				{
					$this->_addressList[] = $address;
				}
			}
		}
	}
	
	
	/**
	* Creates mappings between this and all objects in the Phone List array. Any existing mapping will become orphan(s)
	* @return null
	*/
	function SetPhoneList(&$phoneList)
	{
		$map = new ContactPhoneMap();
		$map->RemoveMapping($this);
		$this->_phoneList = $phoneList;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $contactList
	*/
	function GetPhoneList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$connection = Database::Connect();
		$phone = new Phone();
		$phoneList = Array();
		$this->pog_query = "select distinct * from `phone` a INNER JOIN `contactphonemap` m ON m.phoneid = a.phoneid where m.contactid = '$this->contactId' ";
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " AND ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($phone->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $phone->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $phone->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($phone->pog_attribute_type[$sortBy]['db_attributes']) && $phone->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $phone->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE(a.$sortBy) ";
				}
				else
				{
					$sortBy = "a.$sortBy ";
				}
			}
			else
			{
				$sortBy = "a.$sortBy ";
			}
		}
		else
		{
			$sortBy = "a.phoneid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$cursor = Database::Reader($this->pog_query, $connection);
		while($rows = Database::Read($cursor))
		{
			$phone = new Phone();
			foreach ($phone->pog_attribute_type as $attribute_name => $attrubute_type)
			{
				if ($attrubute_type['db_attributes'][1] != "HASMANY" && $attrubute_type['db_attributes'][1] != "JOIN")
				{
					if ($attrubute_type['db_attributes'][1] == "BELONGSTO")
					{
						$phone->{strtolower($attribute_name).'Id'} = $rows[strtolower($attribute_name).'id'];
						continue;
					}
					$phone->{$attribute_name} = $this->Unescape($rows[strtolower($attribute_name)]);
				}
			}
			$phoneList[] = $phone;
		}
		return $phoneList;
	}
	
	
	/**
	* Associates the Phone object to this one
	* @return 
	*/
	function AddPhone(&$phone)
	{
		if ($phone instanceof Phone)
		{
			if (in_array($this, $phone->contactList, true))
			{
				return false;
			}
			else
			{
				$found = false;
				foreach ($this->_phoneList as $phone2)
				{
					if ($phone->phoneId > 0 && $phone->phoneId == $phone2->phoneId)
					{
						$found = true;
						break;
					}
				}
				if (!$found)
				{
					$this->_phoneList[] = $phone;
				}
			}
		}
	}
	
	
	/**
	* Creates mappings between this and all objects in the Document List array. Any existing mapping will become orphan(s)
	* @return null
	*/
	function SetDocumentList(&$documentList)
	{
		$map = new ContactDocumentMap();
		$map->RemoveMapping($this);
		$this->_documentList = $documentList;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $contactList
	*/
	function GetDocumentList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$connection = Database::Connect();
		$document = new Document();
		$documentList = Array();
		$this->pog_query = "select distinct * from `document` a INNER JOIN `contactdocumentmap` m ON m.documentid = a.documentid where m.contactid = '$this->contactId' ";
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " AND ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($document->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $document->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $document->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($document->pog_attribute_type[$sortBy]['db_attributes']) && $document->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $document->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE(a.$sortBy) ";
				}
				else
				{
					$sortBy = "a.$sortBy ";
				}
			}
			else
			{
				$sortBy = "a.$sortBy ";
			}
		}
		else
		{
			$sortBy = "a.documentid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$cursor = Database::Reader($this->pog_query, $connection);
		while($rows = Database::Read($cursor))
		{
			$document = new Document();
			foreach ($document->pog_attribute_type as $attribute_name => $attrubute_type)
			{
				if ($attrubute_type['db_attributes'][1] != "HASMANY" && $attrubute_type['db_attributes'][1] != "JOIN")
				{
					if ($attrubute_type['db_attributes'][1] == "BELONGSTO")
					{
						$document->{strtolower($attribute_name).'Id'} = $rows[strtolower($attribute_name).'id'];
						continue;
					}
					$document->{$attribute_name} = $this->Unescape($rows[strtolower($attribute_name)]);
				}
			}
			$documentList[] = $document;
		}
		return $documentList;
	}
	
	
	/**
	* Associates the Document object to this one
	* @return 
	*/
	function AddDocument(&$document)
	{
		if ($document instanceof Document)
		{
			if (in_array($this, $document->contactList, true))
			{
				return false;
			}
			else
			{
				$found = false;
				foreach ($this->_documentList as $document2)
				{
					if ($document->documentId > 0 && $document->documentId == $document2->documentId)
					{
						$found = true;
						break;
					}
				}
				if (!$found)
				{
					$this->_documentList[] = $document;
				}
			}
		}
	}
	
	
	/**
	* Gets a list of GotStudentActivity objects associated to this one
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array of GotStudentActivity objects
	*/
	function GetGotstudentactivityList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$gotstudentactivity = new GotStudentActivity();
		$fcv_array[] = array("contactId", "=", $this->contactId);
		$dbObjects = $gotstudentactivity->GetList($fcv_array, $sortBy, $ascending, $limit);
		return $dbObjects;
	}
	
	
	/**
	* Makes this the parent of all GotStudentActivity objects in the GotStudentActivity List array. Any existing GotStudentActivity will become orphan(s)
	* @return null
	*/
	function SetGotstudentactivityList(&$list)
	{
		$this->_gotstudentactivityList = array();
		$existingGotstudentactivityList = $this->GetGotstudentactivityList();
		foreach ($existingGotstudentactivityList as $gotstudentactivity)
		{
			$gotstudentactivity->contactId = '';
			$gotstudentactivity->Save(false);
		}
		$this->_gotstudentactivityList = $list;
	}
	
	
	/**
	* Associates the GotStudentActivity object to this one
	* @return 
	*/
	function AddGotstudentactivity(&$gotstudentactivity)
	{
		$gotstudentactivity->contactId = $this->contactId;
		$found = false;
		foreach($this->_gotstudentactivityList as $gotstudentactivity2)
		{
			if ($gotstudentactivity->gotstudentactivityId > 0 && $gotstudentactivity->gotstudentactivityId == $gotstudentactivity2->gotstudentactivityId)
			{
				$found = true;
				break;
			}
		}
		if (!$found)
		{
			$this->_gotstudentactivityList[] = $gotstudentactivity;
		}
	}
	
	
	/**
	* Gets a list of Company objects associated to this one
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array of Company objects
	*/
	function GetCompanyList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$company = new Company();
		$fcv_array[] = array("contactId", "=", $this->contactId);
		$dbObjects = $company->GetList($fcv_array, $sortBy, $ascending, $limit);
		return $dbObjects;
	}
	
	
	/**
	* Makes this the parent of all Company objects in the Company List array. Any existing Company will become orphan(s)
	* @return null
	*/
	function SetCompanyList(&$list)
	{
		$this->_companyList = array();
		$existingCompanyList = $this->GetCompanyList();
		foreach ($existingCompanyList as $company)
		{
			$company->contactId = '';
			$company->Save(false);
		}
		$this->_companyList = $list;
	}
	
	
	/**
	* Associates the Company object to this one
	* @return 
	*/
	function AddCompany(&$company)
	{
		$company->contactId = $this->contactId;
		$found = false;
		foreach($this->_companyList as $company2)
		{
			if ($company->companyId > 0 && $company->companyId == $company2->companyId)
			{
				$found = true;
				break;
			}
		}
		if (!$found)
		{
			$this->_companyList[] = $company;
		}
	}
	
	
	/**
	* Gets a list of GotProRole objects associated to this one
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array of GotProRole objects
	*/
	function GetGotproroleList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$gotprorole = new GotProRole();
		$fcv_array[] = array("contactId", "=", $this->contactId);
		$dbObjects = $gotprorole->GetList($fcv_array, $sortBy, $ascending, $limit);
		return $dbObjects;
	}
	
	
	/**
	* Makes this the parent of all GotProRole objects in the GotProRole List array. Any existing GotProRole will become orphan(s)
	* @return null
	*/
	function SetGotproroleList(&$list)
	{
		$this->_gotproroleList = array();
		$existingGotproroleList = $this->GetGotproroleList();
		foreach ($existingGotproroleList as $gotprorole)
		{
			$gotprorole->contactId = '';
			$gotprorole->Save(false);
		}
		$this->_gotproroleList = $list;
	}
	
	
	/**
	* Associates the GotProRole object to this one
	* @return 
	*/
	function AddGotprorole(&$gotprorole)
	{
		$gotprorole->contactId = $this->contactId;
		$found = false;
		foreach($this->_gotproroleList as $gotprorole2)
		{
			if ($gotprorole->gotproroleId > 0 && $gotprorole->gotproroleId == $gotprorole2->gotproroleId)
			{
				$found = true;
				break;
			}
		}
		if (!$found)
		{
			$this->_gotproroleList[] = $gotprorole;
		}
	}
	
	
	/**
	* Gets a list of Activity objects associated to this one
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array of Activity objects
	*/
	function GetActivityList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$activity = new Activity();
		$fcv_array[] = array("contactId", "=", $this->contactId);
		$dbObjects = $activity->GetList($fcv_array, $sortBy, $ascending, $limit);
		return $dbObjects;
	}
	
	
	/**
	* Makes this the parent of all Activity objects in the Activity List array. Any existing Activity will become orphan(s)
	* @return null
	*/
	function SetActivityList(&$list)
	{
		$this->_activityList = array();
		$existingActivityList = $this->GetActivityList();
		foreach ($existingActivityList as $activity)
		{
			$activity->contactId = '';
			$activity->Save(false);
		}
		$this->_activityList = $list;
	}
	
	
	/**
	* Associates the Activity object to this one
	* @return 
	*/
	function AddActivity(&$activity)
	{
		$activity->contactId = $this->contactId;
		$found = false;
		foreach($this->_activityList as $activity2)
		{
			if ($activity->activityId > 0 && $activity->activityId == $activity2->activityId)
			{
				$found = true;
				break;
			}
		}
		if (!$found)
		{
			$this->_activityList[] = $activity;
		}
	}
	
	
	/**
	* Gets a list of CRMUser objects associated to this one
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array of CRMUser objects
	*/
	function GetCrmuserList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$crmuser = new CRMUser();
		$fcv_array[] = array("contactId", "=", $this->contactId);
		$dbObjects = $crmuser->GetList($fcv_array, $sortBy, $ascending, $limit);
		return $dbObjects;
	}
	
	
	/**
	* Makes this the parent of all CRMUser objects in the CRMUser List array. Any existing CRMUser will become orphan(s)
	* @return null
	*/
	function SetCrmuserList(&$list)
	{
		$this->_crmuserList = array();
		$existingCrmuserList = $this->GetCrmuserList();
		foreach ($existingCrmuserList as $crmuser)
		{
			$crmuser->contactId = '';
			$crmuser->Save(false);
		}
		$this->_crmuserList = $list;
	}
	
	
	/**
	* Associates the CRMUser object to this one
	* @return 
	*/
	function AddCrmuser(&$crmuser)
	{
		$crmuser->contactId = $this->contactId;
		$found = false;
		foreach($this->_crmuserList as $crmuser2)
		{
			if ($crmuser->crmuserId > 0 && $crmuser->crmuserId == $crmuser2->crmuserId)
			{
				$found = true;
				break;
			}
		}
		if (!$found)
		{
			$this->_crmuserList[] = $crmuser;
		}
	}
	
	
	/**
	* Gets a list of SupportCase objects associated to this one
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array of SupportCase objects
	*/
	function GetSupportcaseList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$supportcase = new SupportCase();
		$fcv_array[] = array("contactId", "=", $this->contactId);
		$dbObjects = $supportcase->GetList($fcv_array, $sortBy, $ascending, $limit);
		return $dbObjects;
	}
	
	
	/**
	* Makes this the parent of all SupportCase objects in the SupportCase List array. Any existing SupportCase will become orphan(s)
	* @return null
	*/
	function SetSupportcaseList(&$list)
	{
		$this->_supportcaseList = array();
		$existingSupportcaseList = $this->GetSupportcaseList();
		foreach ($existingSupportcaseList as $supportcase)
		{
			$supportcase->contactId = '';
			$supportcase->Save(false);
		}
		$this->_supportcaseList = $list;
	}
	
	
	/**
	* Associates the SupportCase object to this one
	* @return 
	*/
	function AddSupportcase(&$supportcase)
	{
		$supportcase->contactId = $this->contactId;
		$found = false;
		foreach($this->_supportcaseList as $supportcase2)
		{
			if ($supportcase->supportcaseId > 0 && $supportcase->supportcaseId == $supportcase2->supportcaseId)
			{
				$found = true;
				break;
			}
		}
		if (!$found)
		{
			$this->_supportcaseList[] = $supportcase;
		}
	}
	
	/**
	 * Needed for array_unique().
	 * Returns the contactId of the object
	 * @return self::contactId
	 */
	function __toString() {
		return $this->contactId;
	}
}
?>